package org.spring.mybatis;

import com.yjp.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

public class YjpBeanFactory implements FactoryBean {
     @Autowired
     private SqlSessionFactory sqlSessionFactory;

     private Class mapperClass;

    public YjpBeanFactory(Class mapperClass){
        this.mapperClass = mapperClass;
    }

    @Override
    public Object getObject() throws Exception{
        sqlSessionFactory.getConfiguration().addMapper(mapperClass);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession.getMapper(mapperClass);
    }

    @Override
    public Class<?> getObjectType(){
        return mapperClass;
    }
}
